<?php
if (!defined('BASEPATH'))
	exit('No direct script access allowed');

//main page to access admin  control
class Ctme_common extends CI_Controller {

	function __construct() {
		parent::__construct();
		$this -> form_validation -> set_error_delimiters('<span class="error">', '</span>');
	}

	// Function to view form login to access backend page
	function index() {
		if ($this -> mod_users -> is_logged_in()) {
			redirect("manage-users");
		} else {
			$this -> form_validation -> set_rules('email', 'lang:login_email', 'callback_login_check');
			$this -> form_validation -> set_error_delimiters('<div class="alert alert-danger" style="position:absolute;top:20px;text-align:center; width:92%;">', '</div>');

			if ($this -> form_validation -> run() == FALSE) {
				$this -> load -> view(B_AUTHOR . 'login');
			} else {
				redirect("manage-users");
			}
		}
	}

	//Function to check user login success or not
	function login_check($email) {
		$password = $this -> input -> post("password");

		if (!$this -> mod_users -> login($email, $password)) {
			$this -> form_validation -> set_message('login_check', $this -> lang -> line('login_invalid_email_and_password'));
			return false;
		}
		return true;
	}

	//Function to exit from system control backend
	public function logout() {
		$this -> mod_users -> logout();
	}

	// Function to display page disallow access page
	public function no_allow_access() {
		$module_id = $this -> uri -> segment(3);
		$data['title'] = "No allow access " . $module_id;
		$data['module'] = $module_id;
		$this -> load -> view(B_MASTER . 'index', $data);
	}
	
	// Function to display page disallow modify page
	public function no_allow_modify() {
		$module_id = $this -> uri -> segment(3);
		$data['title'] = "No allow modify " . $module_id;
		$data['module'] = $module_id;
		$this -> load -> view(B_MASTER . 'index', $data);
	}
	//Function to allow admin to change password
	public function change_password() {
		if (!$this -> mod_users -> is_logged_in()) {
			redirect('ctme_common');
		}

		$data['title'] = $this->lang->line('title_change_password');

		$this -> form_validation -> set_rules('password', 'lang:form_label_new_password', 'trim|min_length[4]|max_length[25|matches[confirm_password]');
		$this -> form_validation -> set_rules('confirm_password', 'lang:form_label_confirm_password', 'trim|min_length[4]|max_length[25]');

		if ($this -> form_validation -> run() === FALSE) {
			$this -> load -> view(B_MASTER . 'index', $data);
		} else {
			$password = $this -> input -> post("password");
			$user_id = $this -> mod_users -> get_logged_in_user_info() -> user_id;

			$is_updated = $this -> mod_globals -> update(PREFIX_TABLE . "users", array('password' => md5($password)), array('user_id' => $user_id));
			if ($is_updated) {
				$this -> session -> set_flashdata('success', 'Your password was changed success.');
				redirect('ctme-common/change-password');
			} else {
				$this -> session -> set_flashdata('error', 'Your password was changed fail.');
				redirect('ctme-common/change-password');
			}
		}
	}

	//Function to view the information of user login to backend page control
	public function my_profile() {
		if (!$this -> mod_users -> is_logged_in()) {
			redirect('ctme_common');
		}

		$data['title'] = $this->lang->line('title_update_profile');
		$user_id = $this -> mod_users -> get_logged_in_user_info() -> user_id;

		$this -> form_validation -> set_rules('username', 'lang:form_label_username', 'required');
		$this -> form_validation -> set_rules('email', 'lang:form_label_email', 'required|valid_email');
		$this -> form_validation -> set_rules('first_name', 'lang:form_label_first_name', 'required');
		$this -> form_validation -> set_rules('last_name', 'lang:form_label_last_name', 'required');

		if ($this -> form_validation -> run() === FALSE) {

			$data['user_data'] = $this -> mod_globals -> select_where(PREFIX_TABLE . "users", array('user_id' => $user_id));
			//$this->load->view('welcome_message',$data);
			$this -> load -> view(B_MASTER . 'index', $data);

		} else {

			// Create users table
			$data_update_users = array('username' => $this -> input -> post('username'), 'first_name' => $this -> input -> post('first_name'), 'last_name' => $this -> input -> post('last_name'), 'email' => $this -> input -> post('email'));

			$is_updated = $this -> mod_globals -> update(PREFIX_TABLE . "users", $data_update_users, array('user_id' => $user_id));
			if ($is_updated) {
				$this -> session -> set_flashdata('success', 'Your profile  was updated success.');
				redirect('ctme-common/my-profile');
			} else {
				$this -> session -> set_flashdata('error', 'Your profile was updated fail.');
				redirect('ctme-common/my-profile');
			}

		}
	}

}
